<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admin extends MY_Controller {

	public function __construct()
    {
        parent:: __construct();
        $this->load->helper("url");
	    $this->load->model('insert_db');
	    $this->load->model('update_db');
        $this->load->model('delete_db');
        $this->load->helper('form');
   		$this->load->library('upload');
        $this->load->library('form_validation');
	}
    
	public function index()
	{
		$this->load->view('admin/menu');
	}
    
    
     public function add_product()
    {
        $this->load->view('admin/add');
    }
    
    public function add_info_product()
    {
		$this->form_validation->set_rules('product_id', 'product_id', 'required');
		$this->form_validation->set_rules('product_type', 'product_type', 'required');
        $this->form_validation->set_rules('product_name', 'product_name', 'required');
        $this->form_validation->set_rules('product_price', 'product_price', 'required|is_natural');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin/add');
		}
		else
		{
            $id_product = $_POST['product_id']; 
            if($this->update_db->check_id_avail($id_product) == 0)
            {
                $data = array('id' => $_POST['product_id'],
                              'type' => $_POST['product_type_branch'],
                              'name' => $_POST['product_name'],
                              'price' => $_POST['product_price'],
                              'cpu' => $_POST['product_cpu'],
                              'ram' => $_POST['product_ram'],
                              'hdd' => $_POST['product_hdd'],
                              'vga' => $_POST['product_vga'],
                              'lcd' => $_POST['product_lcd'],
                              'lan' => $_POST['product_lan'],
                              'weight' => $_POST['product_weight'],
                              'pin' => $_POST['product_pin'],
                              'os' => $_POST['product_os'],
                              'optical' => $_POST['product_optical'],
                              'baohanh' => $_POST['product_baohanh']);
                $data = $this->replace_char($data);
                $this->insert_db->set_product_name($data);
                $this->insert_db->set_product_price($data);
                $this->insert_db->set_product_info($data);
        
                $img_data = $this->do_upload();
                $this->insert_db->set_product_img_path($_POST['product_id'],$img_data, $_POST['product_description']);
                $this->make_thumb('./static/'.$img_data['upload_data']['file_name']);
                
                $thumb_path = 'static/'.$img_data['upload_data']['raw_name'].'_thumb'.$img_data['upload_data']['file_ext'];
                
                $this->insert_db->set_product_thumb_path($_POST['product_id'],$thumb_path,$_POST['product_description']);
                $this->load->view('admin/add_success');
            }
            else
            {
                $data['error'] = 1;
                $this->load->view('admin/add',$data);
            }
        }
    }
    
    function add_product_type_branch($id) 
    {
        $data['options'] = $this->insert_db->get_product_branch($id);
        echo $this->load->view('admin/temp', $data, true);
    }


  
   function do_upload()
	{
		$config['upload_path'] = './static/';
		$config['allowed_types'] = 'gif|jpg|png';
        $config['file_name'] = hash('md5',$_POST['product_name']+ date("hismdY"));
		$config['max_width']  = '0';
		$config['max_height']  = '0';

		$this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->upload->do_upload('product_img_path');

		$data = array('upload_data' => $this->upload->data());
        return $data;
	}
    
    public function replace_char($data)
    {
        $i = 0;
        foreach($data as $content)
        {
            $content = trim($content);
            $content = addslashes($content);
            $data[$i] = $content;
            $i++;
        }
        return $data;
    }

    public function make_thumb($image_path)
    {
        $configthumb['image_library'] = 'gd2';
        $configthumb['source_image']    = $image_path;
        $configthumb['maintain_ratio'] = FALSE;
        $configthumb['create_thumb'] = TRUE;
        $configthumb['thumb_marker'] = "_thumb";
        $configthumb['quality'] = '100';
        $configthumb['width']     = 80;
        $configthumb['height']    = 80;

        $this->load->library('image_lib', $configthumb); 
        $this->image_lib->initialize($configthumb);
        
        $this->image_lib->resize();
        
        unset($configthumb);
    }  
    //Update
     public function update_select()
    {
        $this->load->view('admin/update_select');
    }
    
       public function show_info_product()
    {
        $this->form_validation->set_rules('product_id', 'product_id', 'required|is_natural');
        if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin/update_select');
		}
		else
		{
            $id_product = $_POST['product_id']; 
            if($this->update_db->check_id_avail($id_product) == 1)
            {
                $product_data['id'] = $id_product;              
                $product_data['type_id'] = $this->update_db->get_product_type($id_product);
                $product_data['name'] = $this->update_db->get_product_name($id_product);
                $product_data['price'] = $this->update_db->get_product_price($id_product);
                $product_data['cpu'] = $this->update_db->get_product_info($id_product,1);
                $product_data['ram'] = $this->update_db->get_product_info($id_product,2);
                $product_data['hdd'] = $this->update_db->get_product_info($id_product,3);
                $product_data['vga'] = $this->update_db->get_product_info($id_product,4);
                $product_data['lcd'] = $this->update_db->get_product_info($id_product,5);
                $product_data['lan'] = $this->update_db->get_product_info($id_product,6);
                $product_data['weight'] = $this->update_db->get_product_info($id_product,7);
                $product_data['pin'] = $this->update_db->get_product_info($id_product,8);
                $product_data['os'] = $this->update_db->get_product_info($id_product,9);
                $product_data['optical'] = $this->update_db->get_product_info($id_product,10);
                $product_data['baohanh'] = $this->update_db->get_product_info($id_product,11);
                $product_data['img_path'] = $this->update_db->get_product_img_path($id_product);
                $product_data['description'] = $this->update_db->get_product_img_description($id_product);
                $product_data['options'] = $this->update_db->get_product_branch($id_product);
                $this->load->view('admin/show_info_product',$product_data);
            }
            else
            {
                $data['error'] = 0;
                $this->load->view('admin/update_select',$data);
            } 
        }
    }
        
    public function update_product()
    {
        $id = $_POST['product_id'];
        $data = array($_POST['product_name'],
                      $_POST['product_price'],
                      $_POST['product_cpu'],
                      $_POST['product_ram'],
                      $_POST['product_hdd'],
                      $_POST['product_vga'],
                      $_POST['product_lcd'],
                      $_POST['product_lan'],
                      $_POST['product_weight'],
                      $_POST['product_pin'],
                      $_POST['product_os'],
                      $_POST['product_optical'],
                      $_POST['product_baohanh']);
        $data = $this->replace_char($data);

        $this->update_db->update_product_name($id,$data[0]);
        $this->update_db->update_product_price($id,$data[1]);
        $this->update_db->update_product_info($id,1,$data[2]);
        $this->update_db->update_product_info($id,2,$data[3]);
        $this->update_db->update_product_info($id,3,$data[4]);
        $this->update_db->update_product_info($id,4,$data[5]);
        $this->update_db->update_product_info($id,5,$data[6]);
        $this->update_db->update_product_info($id,6,$data[7]);
        $this->update_db->update_product_info($id,7,$data[8]);
        $this->update_db->update_product_info($id,8,$data[9]);
        $this->update_db->update_product_info($id,9,$data[10]);
        $this->update_db->update_product_info($id,10,$data[11]);
        $this->update_db->update_product_info($id,11,$data[12]);
        
        $img_data = $this->do_upload();
        $this->update_db->update_product_img_path($_POST['product_id'],$img_data, $_POST['product_description']);
        $this->make_thumb('./static/'.$img_data['upload_data']['file_name']);
        
        $thumb_path = './static/'.$img_data['upload_data']['raw_name'].'_thumb'.$img_data['upload_data']['file_ext'];
        
        $this->update_db->update_product_thumb_path($_POST['product_id'],$thumb_path,$_POST['product_description']);

        $this->load->view('admin/update_success');
    }

    

   
    //Delete
     function delete_select()
    {
        $this->load->view('admin/delete_select');
    }
    
    function delete_product()
    {
        $this->form_validation->set_rules('product_id', 'product_id', 'required|is_natural');
        if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin/delete_select');
		}
		else
		{
            $id = $_POST['product_id'];
            if(isset($_POST['product_id']))
            {
                if($this->update_db->check_id_avail($id) == 1)
                {
                    $this->delete_db->delete_product($id);
                    $this->load->view('admin/delete_success');
                }
                else
                {
                    $data['error'] = 0;
                    $this->load->view('admin/delete_select',$data);
                }
            }
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */